package org.jeecg.modules.goods.sdWarehouse.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.jeecg.modules.goods.sdWarehouse.entity.SdWarehouse;
import org.jeecg.modules.goods.sdWarehouse.mapper.SdWarehouseMapper;
import org.jeecg.modules.goods.sdWarehouse.service.ISdWarehouseService;
import org.jeecg.modules.tplus.dto.WarehouseQueryRepDto;
import org.jeecg.modules.tplus.dto.WarehouseQueryReqDto;
import org.jeecg.modules.tplus.service.ITplusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 默认仓库
 * @Author: jeecg-boot
 * @Date:   2025-08-12
 * @Version: V1.0
 */
@Service
public class SdWarehouseServiceImpl extends ServiceImpl<SdWarehouseMapper, SdWarehouse> implements ISdWarehouseService {
    @Autowired
    private ITplusService tplusService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleWarehouse(){
        List<SdWarehouse> sdWarehouseList = this.list();
        if(CollectionUtil.isNotEmpty(sdWarehouseList)) {
            baseMapper.deleteBatchIds(sdWarehouseList);
        }
        List<WarehouseQueryRepDto> warehouseQueryRepDtos = tplusService.warehouseQuery(new WarehouseQueryReqDto());
        if(CollectionUtil.isNotEmpty(warehouseQueryRepDtos)){
            List<SdWarehouse> warehouses = new ArrayList<>();
            warehouseQueryRepDtos.forEach(warehouseQueryRepDto -> {
                SdWarehouse sdWarehouse = new SdWarehouse();
                sdWarehouse.setWarehouseCode(warehouseQueryRepDto.getCode());
                sdWarehouse.setWarehouseName(warehouseQueryRepDto.getName());
                warehouses.add(sdWarehouse);
            });
            super.saveOrUpdateBatch(warehouses);
        }
    }
}
